package org.com.coolfish.scheduler;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.com.coolfish.common.database.entity.KuyuCard;
import org.com.coolfish.common.database.service.KuyuCardService;
import org.com.coolfish.common.message.MsisdnCardMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;

@Service
public class CardShutDownScheduler {

	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
	private KuyuCardService kuyuCardService;

	@Autowired
	private AmqpTemplate rabbitTemplate;

	private final static int CMCC = 1;

	private final static int CTCC = 2;

	private final static int CUCC = 3;

	/**
	 * 27号0点开始处理移动,电信号码数据
	 */
	/*
	 * @Scheduled (cron = "0 0 0 27 * ?") public void scheduledLoadNormalCard() {
	 * KuyuCard kuyuCard = null; handleNormalCard(kuyuCard); }
	 */
	/**
	 * 26号0点开始处理联通号码数据
	 */

	/**
	 * 30-31号0点开始处理号码数据
	 */
	/*
	 * @Scheduled(cron = "0 0 0 30,31 * ?") public void scheduledLoadEndMonth() {
	 * KuyuCard kuyuCard = null; handleEndMonth(kuyuCard); }
	 */

	/**
	 * 1. 查詢卡信息 2.判斷類型是不是正常的卡類 3.判断是不是电信还是移动的
	 * 
	 * 4.封装json数据 5.传到对队列
	 * 
	 * 
	 */
	
	//@Scheduled(cron = "0 0 0 26 * ?")
	@Scheduled(cron ="0 0/2 * * * ?")
	public void startCUCCCard() {
		logger.info("联通开始执行");
		// 查询卡号的类型,id等信息
		List<KuyuCard> list = kuyuCardService.findTypeCardMessage();
		logger.info("联通月套餐卡数量：" + list.size());

		List<KuyuCard> testList = new ArrayList<>();
		for (int i = 0; i < testList.size(); i++) {
			testList.add(list.get(i));
		}

		for (KuyuCard kuyuCard : list) {
			MsisdnCardMessage message = new MsisdnCardMessage(kuyuCard.getTel(), kuyuCard.getZid(),
					kuyuCard.getOperator_type(), kuyuCard.getAmount(), kuyuCard.getIccid(), kuyuCard.getId(),
					kuyuCard.getCard_status(), kuyuCard.getFrequency(), kuyuCard.getOperatorid(),
					kuyuCard.getPackageid());
			String json = JSON.toJSONString(message).toString();
			if (CUCC == kuyuCard.getOperator_type()) {
				rabbitTemplate.convertAndSend("cucc-stop-queue", json);
				logger.info("加入联通月套餐停机队列(cucc-stop-queue),封装的message为：" + json);
			}
		}
	}

	//@Scheduled(cron = "0 0 0 27 * ?")
	@Scheduled(cron ="0 0/2 * * * ?")
	public void startNotTwoORSex() {
		logger.info("非02或6类卡开始执行");
		// 查询卡号的类型,id等信息
		List<KuyuCard> list = kuyuCardService.findTypeCardMessage();
		logger.info("非02或6类月套餐卡数量：" + list.size());

		List<KuyuCard> testList = new ArrayList<>();
		for (int i = 0; i < testList.size(); i++) {
			testList.add(list.get(i));
		}

		// 02或6类卡
		List<Integer> operatoridList = Arrays.asList(185, 188, 191, 193, 226, 160, 170, 182, 204);

		for (KuyuCard kuyuCard : list) {
			// 非02或6类卡操作
			if (!operatoridList.contains(kuyuCard.getOperatorid())) {

				handleNormalCard(kuyuCard);
			}
		}
	}

	//@Scheduled(cron = "0 0 0 30,31 * ?")
	@Scheduled(cron ="0 0/2 * * * ?")
	public void startEndCard() {
		logger.info("02或6类卡开始执行");
		// 查询卡号的类型,id等信息
		List<KuyuCard> list = kuyuCardService.findTypeCardMessage();
		logger.info("02或6类月套餐卡数量：" + list.size());

		List<KuyuCard> testList = new ArrayList<>();
		for (int i = 0; i < testList.size(); i++) {
			testList.add(list.get(i));
		}

		// 02或6类卡
		List<Integer> operatoridList = Arrays.asList(185, 188, 191, 193, 226, 160, 170, 182, 204);

		for (KuyuCard kuyuCard : list) {
			// 非02或6类卡操作
			if (operatoridList.contains(kuyuCard.getOperatorid())) {
				// scheduledLoadEndMonth();
				handleNormalCard(kuyuCard);
			}
		}
	}

	/**
	 * 卡类存放队列
	 * 
	 * @param kuyuCard
	 */

	private void handleNormalCard(KuyuCard kuyuCard) {
		MsisdnCardMessage message = new MsisdnCardMessage(kuyuCard.getTel(), kuyuCard.getZid(),
				kuyuCard.getOperator_type(), kuyuCard.getAmount(), kuyuCard.getIccid(), kuyuCard.getId(),
				kuyuCard.getCard_status(), kuyuCard.getFrequency(), kuyuCard.getOperatorid(), kuyuCard.getPackageid());
		String json = JSON.toJSONString(message).toString();
		if (CMCC == kuyuCard.getOperator_type()) {
			rabbitTemplate.convertAndSend("cmcc-stop-queue", json);
			logger.info("加入移动月套餐停机队列(cmcc-stop-queue),封装的message为：" + json);
		} else if (CTCC == kuyuCard.getOperator_type()) {
			rabbitTemplate.convertAndSend("ctcc-stop-queue", json);
			logger.info("加入电信月套餐停机队列(ctcc-stop-queue),封装的message为：" + json);
		}
	}

}
